package com.hyving.home.jpademo.repository;

import com.hyving.home.jpademo.model.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface StudentDao extends JpaRepository<Student,Long> {
    List<Student> findByName(String name);
    //自定义简单查询
    Student findStudentByNameIs(String name);

    //分页查询
    Page<Student> findAll(Pageable pageable);
    Page<Student> findByName(String name,Pageable pageable);

    //限制查询
    Student findFirstByOrderByNameAsc();
    Student findTopByOrderByAgeDesc();
    Page<Student> queryFirst10ByName(String name, Pageable pageable);
    List<Student> findFirst10ByName(String name, Sort sort);
    List<Student> findTop10ByName(String name, Pageable pageable);


    //原生查询
    @Modifying
    @Query("update Student stu set stu.name = ?1 where stu.id = ?2")
    int modifyNameById(String name, Long id);

    @Transactional
    @Modifying
    @Query("delete from Student  where id = ?1")
    void deleteById(Long id);

}
